package org.zoomdev.zoom.dao.meta;

import java.util.Objects;

public final class IndexMeta {

    private String name;
    private String[] columns;
    private boolean unique;

    public void toSql(StringBuilder sql, boolean pretty) {
        if (pretty) {
            sql.append("  ");
        }

        if (isUnique()) {
            sql.append("UNIQUE ");
        }
        sql.append("INDEX ").append(getName()).append(" (");
        for (int j = 0; j < getColumns().length; j++) {
            if (j > 0) {
                if (pretty) {
                    sql.append(", ");
                } else {
                    sql.append(",");
                }
            }
            sql.append(getColumns()[j]);
        }
        sql.append(")");
    }

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof IndexMeta)) return false;
        IndexMeta indexMeta = (IndexMeta) o;
        return unique == indexMeta.unique && Objects.equals(name, indexMeta.name) && Objects.deepEquals(columns, indexMeta.columns);
    }

    public boolean isUnique() {
        return unique;
    }

    public void setUnique(boolean unique) {
        this.unique = unique;
    }


    public String[] getColumns() {
        return columns;
    }

    public void setColumns(String[] columns) {
        this.columns = columns;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
